<?php
/**
 * Created by PhpStorm.
 * User: MACD
 * Date: 14-7-24
 * Time: 下午4:37
 */

class Info extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->library('session');
        $this->load->helper('url');
        $this->load->helper('form');
        $this->load->library('form_validation');
        $this->load->helper('captcha');
        $this->load->model('info_model');
    }

    public function index()
    {
        $this->load->view('main');
    }

    public function create()
    {
        if(!$this->session->userdata('login'))
        {
            redirect('user/signin');
            return;
        }
        if($this->input->is_ajax_request()&&$this->input->post('phone'))
        {
            $begin = (int)$this->input->post('begin');
            $end = (int)$this->input->post('end');
            $price = 4 + abs($begin - $end);
            $time = $this->input->post('time');
            $phone = $this->input->post('phone');
            $maxnum = $this->input->post('maxnum');
            $key = $this->input->post('key');

            if($key=='0')//车主找乘客
            {
                $arr = array(
                    'USER_ID'=>$this->session->userdata('userid'),
                    'BEGIN_POINT'=>$begin,
                    'END_POINT'=>$end,
                    'U_TIME'=>$time,
                    'PRICE'=>$price,
                    'PHONE'=>$phone,
                    'MAX_NUM'=>$maxnum,
                );
                $info_id = $this->info_model->create_info_sell($arr);
                //var_dump($info_id);
                if($info_id)
                {
                    $this->session->set_userdata('infoid', $info_id);
                    $result = $this->info_model->match_fun(true, $begin, $end, $time, 0, $this->session->userdata('userid'));
                    if($result)
                    {
                        $str =  "<table class='table table-striped'>".
                            "<thead><tr><th>时间</th><th>起点</th><th>终点</th>".
                            "<th>费用</th><th></th></tr></thead>".
                            "<tbody>";
                        foreach($result as $row)
                        {
                            $url = site_url('info/match').'/'.$row['INFO_ID'].'/'.'0';//0表示当前用户为车主，而传递的参数在buy表中
                            $str = $str."<tr><td>".$row['U_TIME']."</td>".
                                "<td>".$this->get_point($row['BEGIN_POINT'])."</td>".
                                "<td>".$this->get_point($row['END_POINT'])."</td>".
                                "<td>".$row['PRICE']."</td>".
                                "<td><a href='$url' onclick=\"alert('由于该系统尚未与通信公司合作，信息反馈操作跳跃')\">匹配</a></td></tr>";
                        }
                        $str = $str.'</tbody></table>';
                        echo $str;
                        return;
                    }
                    else
                    {
                        echo 'nothing';
                        return;
                    }
                }
                else
                {
                    echo '0';
                    return;
                }
            }
            elseif($key=='1')//乘客找车主
            {
                $arr = array(
                    'USER_ID'=>$this->session->userdata('userid'),
                    'BEGIN_POINT'=>$begin,
                    'END_POINT'=>$end,
                    'U_TIME'=>$time,
                    'PRICE'=>$price,
                    'PHONE'=>$phone,
                );
                $info_id = $this->info_model->create_info_buy($arr);
                if($info_id)
                {
                    $this->session->set_userdata('infoid', $info_id);
                    $result = $this->info_model->match_fun(false, $begin, $end, $time, 0, $this->session->userdata('userid'));
                    if($result)
                    {
                        $str =  "<table class='table table-striped'>".
                            "<thead><tr><th>时间</th><th>起点</th><th>终点</th>".
                            "<th>费用</th><th>已加入/可容纳</th><th></th></tr></thead>".
                            "<tbody>";
                        foreach($result as $row)
                        {
                            $url = site_url('info/match').'/'.$row['INFO_ID'].'/'.'1';//1表示当前用户为乘客，参数在sell表中
                            $str = $str."<tr><td>".$row['U_TIME']."</td>".
                                "<td>".$this->get_point($row['BEGIN_POINT'])."</td>".
                                "<td>".$this->get_point($row['END_POINT'])."</td>".
                                "<td>".$row['PRICE']."</td>".
                                "<td>".$row['CURRENT_NUM'].'/'.$row['MAX_NUM'].'</td>'.
                                "<td><a href='$url' onclick=\"alert('由于该系统尚未与通信公司合作，信息反馈操作跳跃')\">匹配</a></td></tr>";
                        }
                        $str = $str.'</tbody></table>';
                        echo $str;
                        return;
                    }
                    else
                    {
                        echo 'nothing';
                        return;
                    }
                }
                else
                {
                    echo '0';
                    return;
                }
            }
            echo '0';
        }
    }

    public function match($info_id, $is_sell)
    {
        if($is_sell=='0')
        {
            $arr = array(
                'INFO_BUY_ID'=>$info_id,
                'INFO_SELL_ID'=>$this->session->userdata('infoid')
            );
            $this->info_model->create_orders($arr);
            $this->load->view('success');
        }
        if($is_sell=='1')
        {
            $arr = array(
                'INFO_SELL_ID'=>$info_id,
                'INFO_BUY_ID'=>$this->session->userdata('infoid')
            );
            $this->info_model->create_orders($arr);
            $this->load->view('success');
        }
    }

    public function manage()
    {
        if($this->session->userdata('login')==true)
        {
            $data = array();
            $data['dsell'] = $this->info_model->get_order_sell($this->session->userdata('userid'));
            $data['dbuy'] = $this->info_model->get_order_buy($this->session->userdata('userid'));
            $data['dfinish'] = $this->info_model->get_order_finished($this->session->userdata('userid'));
            $this->load->view('manage', $data);
        }
        else
        {
            redirect('user/signin');
        }
    }

    public function confirm($id)
    {
        $this->info_model->update_order($id);
        redirect('/info/manage');
    }

    public function new_comment()
    {
        if($this->input->is_ajax_request())
        {
            $orderid = $this->input->post('orderid');
            $id1 = $this->input->post('userid1');
            $id2 = $this->input->post('userid2');
            $content = $this->input->post('content');
            if($id1==$this->session->userdata('userid'))
            {
                $arr = array(
                    'ORDER_ID'=>$orderid,
                    'FROM_USER'=>$id1,
                    'TO_USER'=>$id2,
                    'CONTENT'=>$content
                );
                $this->info_model->create_comment($arr);
            }
            elseif($id2==$this->session->userdata('userid'))
            {
                $arr = array(
                    'ORDER_ID'=>$orderid,
                    'FROM_USER'=>$id2,
                    'TO_USER'=>$id1,
                    'CONTENT'=>$content
                );
                $this->info_model->create_comment($arr);
            }

            $arr = $this->info_model->get_comment($orderid);
            $str = '';
            foreach($arr as $row)
            {
                $t = $row['FROM_ID'].' TO '.$row['TO_ID'];
                $c = $row['CONTENT'];
                $str = $str."<div class='panel panel-default'><div class='panel-heading'><h3 class='panel-title'>$t</h3></div><div class='panel-body'>$c</div></div>";
            }
            echo $str;
        }
    }

    public function comment()
    {
        $orderid = $this->input->get('orderid');
        $arr = $this->info_model->get_comment($orderid);
        $str = '';
        foreach($arr as $row)
        {
            $t = $row['FROM_ID'].' TO '.$row['TO_ID'];
            $c = $row['CONTENT'];
            $str = $str."<div class='panel panel-default'><div class='panel-heading'><h3 class='panel-title'>$t</h3></div><div class='panel-body'>$c</div></div>";
        }

        echo $str;
//        $id1 = $this->input->get('userid1');
//        $id2 = $this->input->get('userid2');
//        if($id1==$this->session->userdata('userid'))
//        {
//            echo '1';
//        }
//        elseif($id2==$this->session->userdata('userid'))
//        {
//            echo '2';
//        }
    }

    private function get_point($num)
    {
        switch($num)
        {
            case 0:
                return '淮工';
            case 1:
                return '月季花园';
            case 2:
                return '大运河桥';
            case 3:
                return '联盛广场';
            case 4:
                return '淮海广场';
        }
        return false;
    }
}
